<template>
  <el-dialog
    :model-value="dialogVisible"
    :title="dialogTitle"
    width="55%"
    top="6px"
    @close="handleClose"
  >
    <el-form :model="form" label-width="100px" :rules="rules" ref="formRef">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username" placeholder="用户名必填" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" placeholder="密码必填" />
      </el-form-item>
      <el-form-item label="身份证号" prop="identity">
        <el-input v-model="form.identity" placeholder="身份证号必填" />
      </el-form-item>
      <el-form-item label="真实姓名" prop="nickname">
        <el-input v-model="form.nickname" placeholder="真实姓名必填" />
      </el-form-item>
      <el-form-item label="生日" prop="birthday">
        <el-input v-model.number="form.birthday" placeholder="生日必填" />
      </el-form-item>
      <el-form-item label="住址" prop="address">
        <el-input v-model.number="form.address" placeholder="住址必填" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email" />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="form.phone" />
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-radio v-model="form.gender" label="M">男</el-radio>
        <el-radio v-model="form.gender" label="G">女</el-radio>
      </el-form-item>
      <el-form-item label="QQ" prop="qq">
        <el-input v-model.number="form.qq" />
      </el-form-item>
      <el-form-item label="微信" prop="wechat">
        <el-input v-model.number="form.wechat" />
      </el-form-item>
      <el-form-item label="头像" prop="icon">
        <el-input v-model.number="form.icon" />
      </el-form-item>
      <el-form-item label="描述" prop="des">
        <el-input v-model.number="form.des" />
      </el-form-item>
      <el-form-item label="所属门店" prop="storeId">
        <el-select v-model="form.storeId" clearable placeholder="请选择" disabled>
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.companyName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工资" prop="salary">
        <el-input v-model.number="form.salary" />
      </el-form-item>
      <el-form-item label="创建时间" prop="createDate">
        <el-date-picker v-model="form.createTime" type="date" style="width: 100%" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="form.status" clearable placeholder="请选择">
          <el-option value="1" label="启用">启用</el-option>
          <el-option value="0" label="禁用">禁用</el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { defineEmits, ref, defineProps, watch, onMounted } from 'vue'
import { save, modify, get } from '../../../api/user'
import { ElMessage } from 'element-plus'
import jwtDecode from 'jwt-decode'
import { listAll } from '@/api/company'

const form = ref({
  email: '',
  phone: '',
  password: '',
  username: '',
  identity: '',
  nickname: '',
  gender: '',
  birthday: '',
  address: '',
  qq: '',
  wechat: '',
  icon: '',
  des: '',
  storeId: '',
  salary: '',
  createTime: '',
  status: ''
})

const options = ref([])
async function Query() {
  // 所属门店
  const query3 = await listAll()
  options.value = query3.data.data
}

// vue组件加载完成后执行
onMounted(() => {
  Query()
})

const props = defineProps({
  // 标题
  dialogTitle: {
    type: String,
    default: '',
    required: true
  },
  // 编辑行数据
  dialogRowValue: {
    type: Object,
    default: () => {}
  }
})
// 监听props.dialogRowValue值改变
watch(
  () => props.dialogRowValue,
  async () => {
    // console.log(props.dialogRowValue)
    // 获取token
    const token = localStorage.getItem('token')
    // 解析获取token
    const decode = jwtDecode(token).id // 解析
    // 根据ID查询登录用户信息
    const resp = await get(decode)
    form.value.storeId = resp.data.data.storeId
    form.value = props.dialogRowValue
  },
  { deep: true, immediate: true }
)

const isDisabled = ref(false)
onMounted(() => {
  if (props.dialogTitle === '编辑') {
    isDisabled.value = true
  }
})

// 表单校验
const rules = ref({
  identity: [
    { required: true, message: '身份证号必填' }
  ],
  nickname: [
    { required: true, message: '真实姓名必填' }
  ],
  birthday: [
    { required: true, message: '生日必填' }
  ],
  address: [
    { required: true, message: '地址必填' }
  ],
  password: [
    { required: true, message: '密码必填' }
  ],
  username: [
    { required: true, message: '用户名必填' }
  ],
  age: [
    { required: true, message: '年龄不能为空' },
    { type: 'number', message: '年龄必须是数字' }
  ]
})

const emits = defineEmits(['update:modelValue', 'query'])

// 关闭
function handleClose() {
  emits('update:modelValue', false)
}
// 确认
const formRef = ref(null)
function handleConfirm() {
  formRef.value.validate(async (valid) => {
    if (valid) {
      props.dialogTitle === '添加' ? await save(form.value) : await modify(form.value)
      ElMessage({
        message: '更新成功',
        type: 'success'
      })
      // 上层组件重新获取数据
      emits('query')
      handleClose()
    } else {
      return false
    }
  })
}
</script>

<style lang="scss" scoped>
::v-deep .el-input__prefix-inner {
  align-items: center;
}
</style>
